<?php

class Gtux_Forms_Setup_Configure extends Zend_Dojo_Form
{
  /**
   * @var ArrayObject
   */
  protected $_valores;
    
  /**
   * 
   */
  public function init()
  {
    $this->setAction('/setup/configurar')
         ->setAttrib('id', 'frmVariables')
         ->setMethod('post');
    
    $this->setDecorators(array(
        'FormElements',
        array('HtmlTag', array('tag' => 'table')),
        'DijitForm'
    ));
        
    $valores = $this->_valores;
    $elements = new ArrayObject();
    // host
    $variable = $valores[Gtux_Model_Variable::CONFIG_HOST];
    $element = new Zend_Dojo_Form_Element_TextBox('host');
    $element->setAllowEmpty(false)
            ->setAttrib('id', 'txtHost')
            ->setAutoInsertNotEmptyValidator(TRUE)
            ->setLabel('Host')
            ->setRequired(false)
            ->setOrder(1)
            ->setTrim(true)
            ->setMaxLength(255)
            ->addValidator(new Zend_Validate_StringLength(4));
    if ($variable instanceof Gtux_Model_Variable) 
    {
      $element->setValue($variable->getValor());
    }                                
    $elements[] = $element;
    
    // user
    $variable = $valores[Gtux_Model_Variable::CONFIG_USUARIO];
    $element = new Zend_Dojo_Form_Element_TextBox('user');
    $element->setAllowEmpty(false)
            ->setAttrib('id', 'txtUser')
            ->setAutoInsertNotEmptyValidator(TRUE)
            ->setLabel('Usuario')
            ->setRequired(false)
            ->setOrder(2)
            ->setTrim(TRUE)
            ->setMaxLength(50)
            ->addValidator(new Zend_Validate_StringLength(4));
    if ($variable instanceof Gtux_Model_Variable) 
    {
      $element->setValue($variable->getValor());
    }
    $elements[] = $element;

    // password
    $variable = $valores[Gtux_Model_Variable::CONFIG_CLAVE];
    $element = new Zend_Dojo_Form_Element_PasswordTextBox('clave');
    $element->setAllowEmpty(false)
            ->setAttrib('id', 'txtClave')
            ->setAutoInsertNotEmptyValidator(TRUE)
            ->setLabel('Clave')
            ->setRequired(false)
            ->setOrder(3)
            ->setTrim(TRUE)
            ->setMaxLength(50)
            ->addValidator(new Zend_Validate_StringLength(4));
    if ($variable instanceof Gtux_Model_Variable) 
    {
      $element->setValue($variable->getValor());
    }
    $elements[] = $element;
    
    // base de datos
    $variable = $valores[Gtux_Model_Variable::CONFIG_BASEDATOS];
    $element = new Zend_Dojo_Form_Element_TextBox('base_datos');
    $element->setAllowEmpty(false)
            ->setAttrib('id', 'txtBaseDatos')
            ->setAutoInsertNotEmptyValidator(TRUE)
            ->setLabel('Base de datos')
            ->setRequired(false)
            ->setOrder(4)
            ->setTrim(TRUE)
            ->setMaxLength(50)
            ->addValidator(new Zend_Validate_StringLength(4));
    if ($variable instanceof Gtux_Model_Variable) 
    {
      $element->setValue($variable->getValor());
    }
    $elements[] = $element;
    
    // editar
    $element = new Zend_Form_Element_Hidden('editar');
    $element->setAttrib('id', 'hdnEditar')
            ->setValue('1');
    $elements[] = $element;            
    
    // creo el hash contra CSRF
    $nocsrf = new Zend_Form_Element_Hash('token');
    $nocsrf->setSalt(get_class($this).Zend_Registry::get('salt'));
    $nocsrf->setTimeout(3600);
    $elements[] = $nocsrf;

    // creo el boton de submit
    $submit = new Zend_Dojo_Form_Element_SubmitButton('Enviar');
    $submit->setAttrib('id', 'smbEnviar')
           ->setOrder(5)
           ->setDecorators(array(
             'DijitElement',
             array('decorator' => array('td' => 'HtmlTag'), 'options' => array('tag' => 'td', 'colspan' => 2, 'style' => 'text-align: center;')),
             array('decorator' => array('tr' => 'HtmlTag'), 'options' => array('tag' => 'tr'))
           ));       
    
    foreach ($elements as $item) 
    {
      $item->setDecorators(array(
        'DijitElement',
        'Description',
        'Errors',
        array('decorator' => array('td' => 'HtmlTag'), 'options' => array('tag' => 'td')),
        array('Label', array('tag' => 'td')),
        array('decorator' => array('tr' => 'HtmlTag'), 'options' => array('tag' => 'tr')),
      ));
      $this->addElement($item);
    }
    $this->addElement($submit);
  }
  
  /**
   * Establece los valores para inicializar los campos
   * 
   * @param ArrayObject $valores
   */
  public function setValores(ArrayObject $valores) 
  {
    $this->_valores = $valores;
  }
  
}

?>